package com.myfir.action.user;

import com.myfir.action.BaseAction;
import com.myfir.entity.User;
import com.myfir.service.user.UserService;

public class UserLoginAction extends BaseAction {
	private String password;
	private String name;
	private UserService userService;
	public static User superUser;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public static User getSuperUser() {
		return superUser;
	}

	public static void setSuperUser(User superUser) {
		UserLoginAction.superUser = superUser;
	}

	public String execute() {
		try {
			User user = userService.findByName(name);
			if (name.isEmpty()) {
				request.setAttribute("user_login_error1", "username can not be null");
				System.out.print("fail");
				return "fail";
			}
			if (user == null) {
				request.setAttribute("user_login_error2", "username is not exist");
				System.out.print("fail");
				return "fail";
			}
			String pwd = password;
			if (!name.equals(user.getName()) || !pwd.equals(user.getPassword())) {
				request.setAttribute("user_login_error3", "wrong password");
				System.out.print("fail");
				return "fail";
			}
			session.put("user", user);
			superUser = user;
			System.out.print("success");
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "fail";
	}
}
